package com.zby.charity.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.charity.common.ErrorCode;
import com.zby.charity.dao.CommentsDao;
import com.zby.charity.entity.CommentsEntity;
import com.zby.charity.entity.PostsEntity;
import com.zby.charity.exception.BusinessException;
import com.zby.charity.service.CommentsService;
import com.zby.charity.service.PostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service("commentsService")
public class CommentsServiceImpl extends ServiceImpl<CommentsDao, CommentsEntity> implements CommentsService {

    @Autowired
    private PostsService postsService;
    @Override
    public boolean addComments(CommentsEntity comment) {
        Integer pid = comment.getPid();
        if(pid<=0)
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        PostsEntity byId = postsService.getById(pid);
        if(byId == null)
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "帖子不存在");
        if(comment.getContent() == null || comment.getContent().length() == 0)
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评论内容不能为空");
        return this.save(comment);
    }
}
